package com.mocchira.music.radio;

import java.net.URLDecoder;

import org.json.JSONArray;

import com.google.ads.AdRequest;

import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TabHost;

public class HotTagsActivity extends TabActivity {

    private HotTagsManager manager;
    private ProgressDialog progress = null;
    
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            JSONArray hotList = manager.getHotTags();
            if (hotList == null) {
                this.sendEmptyMessageDelayed(0, 1000);
                return;
            }
            JSONArray selList = manager.getSelectableTags();
            if (selList == null) {
                this.sendEmptyMessageDelayed(0, 1000);
                return;
            }
            
            TabHost tabHost = getTabHost();  // The activity TabHost
            // delete dummy tab
            tabHost.clearAllTabs();
            tabHost.setVisibility(View.VISIBLE);
            TabHost.TabSpec spec;  // Resusable TabSpec for each tab
            Intent intent;  // Reusable Intent for each tab
            Bundle bundle;
            for (int i = 0; i < hotList.length(); i++) {
                try {
                    String tag = hotList.getString(i);
                    String dec = URLDecoder.decode(tag, "UTF-8");

                    // Create an Intent to launch an Activity for the tab (to be reused)
                    intent = new Intent().setClass(HotTagsActivity.this, HotTagsPlayListActivity.class);
                    bundle = new Bundle();
                    bundle.putString(R.string.bundle_artist+"", dec);
                    intent.putExtras(bundle);

                    // Initialize a TabSpec for each tab and add it to the TabHost
                    spec = tabHost.newTabSpec(tag).setIndicator(tag)
                                  .setContent(intent);
                    tabHost.addTab(spec);
                    
                } catch (Exception ex) {};
            }
            tabHost.setCurrentTab(0);
            if (progress.isShowing())
                try {progress.dismiss();}catch (Exception nex){};
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hot);
        manager = HotTagsManager.getInstance(this);
        manager.getHotTags();
        manager.getSelectableTags();
        handler.sendEmptyMessageDelayed(0, 1);
        // insert dummy tab
        TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("dummy").setIndicator("dummy").setContent(new Intent().setClass(HotTagsActivity.this, HotTagsPlayListActivity.class)));
        tabHost.setCurrentTab(0);
        tabHost.setVisibility(View.INVISIBLE);
        
        //AdRequest request = new AdRequest();
        //request.addTestDevice(AdRequest.TEST_EMULATOR);
        
        this.progress = ProgressDialog.show(HotTagsActivity.this, "preparing...", "Download Artist List File");
    }

}
